package com.sunyur.tailor.channel.web.outer.message.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.common.outer.constants.OuterConstant;
import com.sunyur.tailor.channel.common.outer.enums.OuterResultEnum;
import com.sunyur.tailor.channel.service.middle.org.MiddleOrgService;
import com.sunyur.tailor.channel.service.middle.user.MiddleUserService;
import com.sunyur.tailor.channel.service.outer.org.param.OuterOrgParam;
import com.sunyur.tailor.channel.service.outer.user.param.OuterUserParam;
import com.sunyur.tailor.channel.web.outer.message.dto.HrMessageDTO;
import com.sunyur.tailor.channel.web.outer.message.vo.HrMessageVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.BeanUtilsBean2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 *
 * @Author 99288
 * @Description 组织&人员消息接口
 * @Date 16:15 2021/7/15
 * @Param
 * @return
**/
@Log4j2
@RestController
@RequestMapping("/outer/hr")
public class HrMessageController {
    /**
     * 人员服务
     */
    @Autowired
    private MiddleUserService middleUserService;
    /**
     * 组织服务
     */
    @Autowired
    private MiddleOrgService middleOrgService;

    /**
     * 接收消息
     */
    @PostMapping(value = "/incrEntrance", produces = MediaType.APPLICATION_JSON_VALUE)
    public HrMessageVO incrEntrance(@RequestBody @Valid HrMessageDTO hrMessageDTO) throws InvocationTargetException, IllegalAccessException {
        log.info("HrController entrance param:{}", JSON.toJSONString(hrMessageDTO));
        ChannelResult result = new ChannelResult<>();
        Integer dataType = hrMessageDTO.getDataType();
        //根据类型判断是组织还是人员
        if(OuterConstant.HR_USER.equals(dataType)){
            OuterUserParam outerUserParam = new OuterUserParam();
            BeanUtilsBean2.getInstance().copyProperties(outerUserParam, hrMessageDTO);
            //调用同步人员
            result = middleUserService.syncUser(outerUserParam);
        }else if(OuterConstant.HR_ORG.equals(dataType)){
            OuterOrgParam outerOrgParam = new OuterOrgParam();
            BeanUtilsBean2.getInstance().copyProperties(outerOrgParam, hrMessageDTO);
            //调用同步组织
            result = middleOrgService.syncOrg(outerOrgParam);
        }
        HrMessageVO hrMessageVO;
        //根据同步结果进行返回
        if(ChannelResultUtil.isSuccess(result)){
            hrMessageVO = HrMessageVO.builder().success(Boolean.TRUE).code(OuterResultEnum.SUCCESS.getCode()).build();
            log.info("HrController entrance return success:{}", JSON.toJSONString(result));
        }else{
            hrMessageVO = HrMessageVO.builder().success(Boolean.FALSE).code(OuterResultEnum.FAILED.getCode()).msg(OuterResultEnum.FAILED.getValue()).build();
            log.error("HrController entrance return failed:{}", JSON.toJSONString(result));
        }
        return hrMessageVO;
    }

    /**
     * 接收消息
     */
    @PostMapping(value = "/allEntrance", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult allEntrance(@RequestBody @Valid HrMessageDTO hrMessageDTO) throws InvocationTargetException, IllegalAccessException {
        log.info("HrController entrance param:{}", JSON.toJSONString(hrMessageDTO));
        ChannelResult result = new ChannelResult<>();
        Integer dataType = hrMessageDTO.getDataType();
        //根据类型判断是组织还是人员
        if(OuterConstant.HR_USER.equals(dataType)){
            OuterUserParam outerUserParam = new OuterUserParam();
            //调用同步人员
            result = middleUserService.syncUser(outerUserParam);
        }else if(OuterConstant.HR_ORG.equals(dataType)){
            OuterOrgParam outerOrgParam = new OuterOrgParam();
            //调用同步组织
            result = middleOrgService.syncOrg(outerOrgParam);
        }
        return result;
    }
}